package cn.iocoder.yudao.module.crm.controller.admin.tasktype;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.tasktype.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.tasktype.TaskTypeDO;
import cn.iocoder.yudao.module.crm.service.product.CrmProductService;
import cn.iocoder.yudao.module.crm.service.tasktype.TaskTypeApproverService;
import cn.iocoder.yudao.module.crm.service.tasktype.TaskTypeAssigneeService;
import cn.iocoder.yudao.module.crm.service.tasktype.TaskTypeContentService;
import cn.iocoder.yudao.module.crm.service.tasktype.TaskTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - CRM 任务单类型")
@RestController
@RequestMapping("/crm/task-type")
@Validated
public class TaskTypeController {

    @Resource
    private TaskTypeService taskTypeService;
    @Resource
    private TaskTypeAssigneeService taskTypeAssigneeService;
    @Resource
    private TaskTypeApproverService taskTypeApproverService;
    @Resource
    private TaskTypeContentService taskTypeContentService;
    @Resource
    private CrmProductService productService;

    @PostMapping("/create")
    @Operation(summary = "新增CRM 任务单类型")
    @PreAuthorize("@ss.hasPermission('crm:task-type:create')")
    public CommonResult<Long> createTaskType(@Valid @RequestBody TaskTypeDetailReqVO createReqVO) {
        return success(taskTypeService.createTaskType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新CRM 任务单类型")
    @PreAuthorize("@ss.hasPermission('crm:task-type:update')")
    public CommonResult<Long> updateTaskType(@Valid @RequestBody TaskTypeDetailReqVO updateReqVO) {
        return success(taskTypeService.updateTaskType(updateReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得CRM 任务单类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:task-type:query')")
    public CommonResult<TaskTypeDetailRespVO> getTaskType(@RequestParam("id") Long id) {
        TaskTypeDO taskType = taskTypeService.getTaskType(id);
        TaskTypeDetailRespVO taskTypeDetailRespVO = new TaskTypeDetailRespVO();
        BeanUtils.copyProperties(taskType, taskTypeDetailRespVO);
        taskTypeDetailRespVO
                .setApprover(taskTypeApproverService.getByTaskTypeId(id))
                .setAutoIssue(taskTypeAssigneeService.getByTaskTypeId(id))
                .setContent(taskTypeContentService.getByTaskTypeId(id));
        return success(taskTypeDetailRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得CRM 任务单类型分页")
    @PreAuthorize("@ss.hasPermission('crm:task-type:query')")
    public CommonResult<PageResult<TaskTypePageRespVO>> getTaskTypePage(@Valid TaskTypePageReqVO pageReqVO) {
        PageResult<TaskTypeDO> pageResult = taskTypeService.getTaskTypePage(pageReqVO);
        PageResult<TaskTypePageRespVO> voPageResult = new PageResult<>();
        List<TaskTypeDO> list = pageResult.getList();
        voPageResult.setList(convertList(list, taskType -> new TaskTypePageRespVO()
                .setId(taskType.getId()).setName(taskType.getName())
                .setProductName(productService.getProduct(taskType.getProductId()).getName())
                .setDescription(taskType.getDescription())
                .setCreateTime(taskType.getCreateTime())
                .setCreator(taskType.getCreator())
                .setAutoIssueNum(taskTypeAssigneeService.getAutoIssueNum(taskType.getId()))));
        voPageResult.setTotal(pageResult.getTotal());
        return success(voPageResult);
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得任务单类型列表", description = "只包含任务单类型")
    @PreAuthorize("@ss.hasPermission('crm:task-type:query')")
    public CommonResult<List<TaskTypeRespVO>> getTaskTypeSimpleList() {
        List<TaskTypeDO> list = taskTypeService.getTaskTypeList();
        return success(convertList(list, taskType -> new TaskTypeRespVO().setId(taskType.getId()).setName(taskType.getName())
                .setProductId(taskType.getProductId())));
    }

    @GetMapping("/down-list")
    @Operation(summary = "获取任务单类型下拉列表", description = "只包含任务单类型，主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('crm:task-type:query')")
    public CommonResult<List<TaskTypeDownRespVO>> getTaskTypeDownList() {
        List<TaskTypeDO> list = taskTypeService.getTaskTypeList();
        return success(convertList(list, taskType -> new TaskTypeDownRespVO().setId(taskType.getId()).setName(taskType.getName())));
    }
}